

package com.springdemo.tools;


import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串工具类
 * @author ss
 * @date   2013-10-10
 */
public class StringHelper {
	
	/**
	 * 处理字符为null
	 * @Title: toTrim
	 * @data:2014-2-17下午1:30:05
	 * @author:san
	 *
	 * @param str
	 * @return
	 */
	public static String toTrim(String str)
	  {
	    if (str == null) {
	      return "";
	    }
	    if (str.trim().equalsIgnoreCase("null")) {
	      return "";
	    }
	    return str.trim();
	  }

	/**
	 * 判断是否为空字符串 null 和   "" 都返回 true
	 * 
	 * @author 
	 * @param s
	 * @return boolean
	 */
	public static boolean isEmpty(String s) {
		if (null != s && !("").equals(s)) {
			return false;
		}
		return true;
	}
	
	
		/**
		 * 字符排序
		 * @Title: Sort
		 * @data:2015-11-26下午04:17:35
		 * @author:san
		 *
		 * @param str
		 * @return
		 */
	  public static String Sort(String str)
	  {
	    String value = "";
	    if (str != null) {
	      char[] chr = str.toCharArray();
	      Arrays.sort(chr);
	      value = new String(chr);
	    }
	    return value;
	  }
	
	
	/**
	 * 判断是否是数字
	 * @Title: isDigit
	 * @data:2014-2-17下午1:24:50
	 * @author:san
	 *
	 * @param srcStr
	 * @return
	 */
	public static boolean isDigit(String srcStr) {
		if (srcStr == null) {
			return false;
		}
		int srcStrLenght = srcStr.length();
		if (srcStrLenght <= 0) {
			return false;
		}
		for (int i = 0; i < srcStrLenght; i++) {
			if (i == 0 && srcStr.charAt(i) == '-') {
				continue;
			}
			if (!Character.isDigit(srcStr.charAt(i))) {
				return false;
			}
			return true;
		}
		return false;
	}
	 /***
     * 切断字符串 （没有省略号）
     * @param input
     * @param length
     * @return
     */
    public static String formatStringNoPoint(String input, int length) {
		String temp = "<SPAN title=\"" + input + "\">";
		temp += StringHelper.formatString(input, length);
		temp += "</SPAN>";
		return temp;
	}
    /**
     * 切断字符串
     * @param msg
     * @param fixLength
     * @return
     */
    public static String formatString(String msg, int fixLength) {
		if (msg == null)
			msg = "";
		if (msg.trim().equals("null") || msg.trim().equals(""))
			msg = "";
		byte data[] = msg.getBytes();
		if (data.length <= fixLength)
			return msg;
		byte tmp[] = new byte[fixLength];
		System.arraycopy(data, 0, tmp, 0, fixLength);
		data = tmp;
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			if (data[data.length - 1 - i] >= 0)
				break;
			count++;
		}

		switch (count % 2) {
		case 1: // '\001'
			byte tmp2[] = new byte[data.length - 1];
			System.arraycopy(data, 0, tmp2, 0, data.length - 1);
			data = tmp2;
			break;
		}
		String reSult = new String(data);
		return reSult;
	}
    /**
     * 在页面上显示时把空字符串或null转化成&nbsp;
     * @param src
     * @return
     */
    public static String getNullToNbsp(String src){
    	String result = "";
    	if(StringHelper.toTrim(src).equals("")){
    		result = "&nbsp;";
    	}else{
    		result = src;
    	}
    	return result;
    }
    public static String getZeroToLine(int num){
    	String result = "";
    	if(num==0){
    		result = "-";
    	}else{
    		result = num+"";
    	}
    	return result;
    }
 
    /**
	 * 将模板中的变量替换成相应的栏目数据
	 * @param variable
	 * @param
	 * @return
	 */
	public static String replaceValue(String templateContent,String variable,String value){
		variable="${"+variable+"}$";
		templateContent=templateContent.replace(variable, value);
		return templateContent;
	}
	/**
	 * 获取来原信息
	 * @Title: getSourceType
	 * @data:2012-7-20上午10:31:55
	 * @author:zxd
	 *
	 * @param webSiteName
	 * @param sourceName
	 * @return
	 */
	public static String  getSourceName(String webSiteName,String sourceName){
		String result="";
		if(StringHelper.toTrim(sourceName).equals("") 
				|| StringHelper.toTrim(sourceName).equals("本网站")
				){
				result=webSiteName;
		}else{
			result = sourceName;
		}
		return result;
		
	}
	
	
  /**
   * 
   * @Title: stringFilter
   * @data:2014-2-18上午10:49:09
   * @author:san
   *
   * @param str
   * @return
   */
	public static String stringFilter(String str){
		if(str!=null){
			str = str.replaceAll(">", "&gt;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll("\"", "&quot;");
			return str;
		}
		return "";
	}
	
	
	 /**
	  * 
	  * @Title: getIdsStr
	  * @data:2014-2-18上午10:48:48
	  * @author:san
	  *
	  * @param idList
	  * @return
	  */
	public static String getIdsStr(List<Integer> idList) {
		StringBuffer sb = new StringBuffer("");
		if(idList!=null && idList.size()>0){
			for (Integer id : idList) {
				sb.append(id+",");
			}
			sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString();
	}

    public static boolean isReName(String src) {
        boolean result = false;
        if (src != null && !src.equals("")) {
            Matcher ma = Pattern.compile("^[a-zA-z]\\w{3,15}$").matcher(src);
            while (ma.find()) {
                result = true;
            }
        }
        return result;
    }
	
    
    /** 
     * 获得指定文件的byte数组 
     */  
    public static byte[] getBytes(String filePath){  
        byte[] buffer = null;  
        try {  
            File file = new File(filePath);  
            FileInputStream fis = new FileInputStream(file);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
            byte[] b = new byte[1000];  
            int n;  
            while ((n = fis.read(b)) != -1) {  
                bos.write(b, 0, n);  
            }  
            fis.close();  
            bos.close();  
            buffer = bos.toByteArray();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return buffer;  
    }
    
	private static String isOuShu(int width){
		String targetWidth = width+"";
		if(width>0){
			if(width%2==0){
				targetWidth = width+"";
			}else{
				int widthOu = width+1;
				if(widthOu%2==0){
					targetWidth = widthOu+"";
				}
			}
		}
		return targetWidth;
	}

	/**
	 * unicode编码
	 * 
	 * @Title: encode
	 * @data:2015-12-21上午9:57:35
	 * @author:wolf
	 * 
	 * @param theString
	 * @return
	 */
	public static String encode(String theString) {
		if(!StringHelper.toTrim(theString).equals("")){
			StringBuilder sb = new StringBuilder(theString.length() * 3);
			for (char c : theString.toCharArray()) {
				if (c < 256) {
					sb.append(c);
				} else {
					sb.append("\\u");
					sb.append(Character.forDigit((c >>> 12) & 0xf, 16));
					sb.append(Character.forDigit((c >>> 8) & 0xf, 16));
					sb.append(Character.forDigit((c >>> 4) & 0xf, 16));
					sb.append(Character.forDigit((c) & 0xf, 16));
				}
			}
			return sb.toString();
		}
		return "";
	}

	/**
	 * unicode解码
	 * 
	 * @Title: decodeUnicode
	 * @data:2015-12-21上午9:57:36
	 * @author:wolf
	 * 
	 * @param
	 * @return
	 */
	public static String decodeUnicode(String s) {
		if(!StringHelper.toTrim(s).equals("")){
			StringBuilder sb = new StringBuilder(s.length());
			char[] chars = s.toCharArray();
			for (int i = 0; i < chars.length; i++) {
				char c = chars[i];
				if (c == '\\' && chars[i + 1] == 'u') {
					char cc = 0;
					for (int j = 0; j < 4; j++) {
						char ch = Character.toLowerCase(chars[i + 2 + j]);
						if ('0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f') {
							cc |= (Character.digit(ch, 16) << (3 - j) * 4);
						} else {
							cc = 0;
							break;
						}
					}
					if (cc > 0) {
						i += 5;
						sb.append(cc);
						continue;
					}
				}
				sb.append(c);
			}
			return sb.toString();
		}
		return "";
	}
	
	/**
     * 正则表达式校验邮箱
     * @param
     * @return 匹配成功返回true 否则返回false;
     */
    public static boolean checkEmail(String email){
        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        //正则表达式的模式
        Pattern p = Pattern.compile(RULE_EMAIL);
        //正则表达式的匹配器
        Matcher m = p.matcher(email);
        //进行正则匹配
        return m.matches();
    }
    /**
     * 判断是否是数字
     * @Title: isNumeric
     * @data:2017年4月19日下午3:59:36
     * @author:lysk1
     *
     * @param str
     * @return 是 ：true 否 false
     */
    public static boolean isNumeric(String str){ 
    	   Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]+)?$"); 
    	   Matcher isNum = pattern.matcher(str);
    	   if( !isNum.matches() ){
    	       return false; 
    	   } 
    	   return true; 
    	}
    /**
     * 判断是否是汉字
     * @Title: isChinese
     * @data:2017年4月19日下午3:59:36
     * @author:lysk1
     *
     * @param str
     * @return 是 ：true 否 false
     */
    public static boolean isChinese(String str){ 
    	Pattern pattern = Pattern.compile("^[\u0391-\uFFE5]+$"); 
    	Matcher isNum = pattern.matcher(str);
    	if( !isNum.matches() ){
    		return false; 
    	} 
    	return true; 
    }

	public static void main(String[] args) {
/*//		String webSite="新浪网";
//		String sourceType="本网站";
//		StringHelper st = new StringHelper();
//		String ss=	st.getSourceType(webSite, sourceType);
//		System.out.println(ss);
        String msg = "中华人民共和国在今天成立了，五星红旗迎风飘扬，胜利歌声多么响亮。越过高手，越过平原，跨国长江黄河长江，宽广美丽的土地，使我们亲爱的家乡，英雄的人民，站起来，歌唱我们亲爱的祖国。";
        String a = StringHelper.formatString(msg,20);
        System.out.print(a);*/
		
		String targetWdith = StringHelper.isOuShu(1033);
		
		System.out.println(targetWdith);
		

	}
	
    
}
